package morncloud.com.notes.ui;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import morncloud.com.notes.R;
import morncloud.com.notes.database.DAO;
import morncloud.com.notes.database.Provider;


/**
 * Created by majoy on 2015/1/7.
 */
public class NotesGridCursorAdapter extends CursorAdapter implements DataChangeCallbacks {
    private Context mContext;

    public NotesGridCursorAdapter(Context context, Cursor c) {
        super(context, c);
        this.mContext = context;
    }

    public NotesGridCursorAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    public NotesGridCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.notes_grid_fragment_item, parent, false);
        viewHolder.data = (TextView) view.findViewById(R.id.notes_grid_item_text);
        viewHolder.alert = (ImageView) view.findViewById(R.id.notes_grid_item_alert);
        viewHolder.date = (TextView) view.findViewById(R.id.notes_grid_item_date);
        viewHolder.img = (ImageView) view.findViewById(R.id.notes_grid_item_img);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view =super.getView(position, convertView, parent);
        if (convertView==null){
            Log.e("SYJ","convertView is null");
        }
        updateBG(position,view,(AbsListView)parent);
        return view;
    }
    private void updateBG(int position, View convertView, AbsListView parent){
        if (parent.isItemChecked(position)){
            convertView.setBackground(new ColorDrawable(Color.BLUE));
        } else {
            convertView.setBackground(new ColorDrawable(Color.GRAY));
        }
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.data.setText(cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.DATA)));
        viewHolder.date.setText(cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.CREATE_TIME)));
    }

    @Override
    public void reloadView() {
        this.changeCursor(DAO.getInstance(mContext).getAllNotes());
    }

    public static class ViewHolder {
        public TextView data;
        public ImageView img;
        public TextView date;
        public ImageView alert;
    }
}
